package com.sss.myspringsecurity.exception;

import com.sss.myspringsecurity.util.ResponseDataEnum;
import com.sss.myspringsecurity.util.Result;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 
 * @author ruoyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler
{

    /**
     * 权限不足异常处理
     * @param e
     * @return com.sss.myspringsecurity.util.Result
     * @author WYY-SSS
    **/
    @ExceptionHandler(AccessDeniedException.class)
    public Result handleAuthorizationException(AccessDeniedException e)
    {
        return Result.fail(ResponseDataEnum.NOTAUTHOR);
    }

    /**
     * 用户错误异常处理
     * @param e
     * @return com.sss.myspringsecurity.util.Result
     * @author SSS
     **/
    @ExceptionHandler(BadCredentialsException.class)
    public Result handleUsernameNotFoundException(BadCredentialsException e)
    {
        return Result.fail(ResponseDataEnum.NOTUSER);
    }

    @ExceptionHandler(CustomException.class)
    public Result businessException(CustomException e)
    {
        if (StringUtils.isEmpty(e.getCode()))
        {
            return Result.fail(e.getMessage());
        }
        return Result.fail(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e)
    {
        return Result.fail();
    }




}
